package com.wrk._2_数组._02_习题;

import java.util.Arrays;
import java.util.Random;

public class _005_求平均值和排序 {
    //创建一个generateUniqueRandoms（生成唯一随机数）方法，操作数组
    public static int[] generateUniqueRandoms(int min, int max, int count) {
        //定义一个数组
        int[] array = new int[max - min];
        //遍历循环，为数组填充有序序列
        for (int i = 0; i < array.length; i++) {
            array[i] = min + i;
        }
        //打乱顺序
        int temp = 0;
        for (int i = 0; i < array.length; i++) {
            int sum = new Random().nextInt(array.length - 1);
            temp = array[i];
            array[i] = array[sum];
            array[sum] = temp;
        }
        //输出数组前count个元素
        return Arrays.copyOf(array, count);
    }

    //创建main方法，调用generateUniqueRandoms方法运行结果
    public static void main(String[] ages) {
        //定义一个数组，min:最小值0，max:最大值100，count：前10个元素
        int[] randoms = generateUniqueRandoms(0, 100, 10);
        //求平均值
        int sum =0;
        for (int i = 0; i < randoms.length; i++) {
            sum += randoms[i];
        }
        double averageValue =sum/randoms.length;
        System.out.println("不重复随机数: " + Arrays.toString(randoms));
        System.out.println("数组平均值为："+averageValue);
    }
}
